package cn.mumzone.pos.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputFilter;
import android.text.LoginFilter;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import cn.mumzone.basic.base.BaseActivity;
import cn.mumzone.basic.util.PatternUtils;
import cn.mumzone.basic.util.ToastUtil;
import cn.mumzone.pos.R;
import cn.mumzone.pos.presenter.UpdatePwdPresenter;
import cn.mumzone.pos.presenter.contract.UpdatePwdContract;

/**
 * author:        yupeng
 * created on:    2019/7/13 00:45
 * description:   修改密码
 */
public class ACT_UpdatePwd extends BaseActivity<UpdatePwdPresenter> implements UpdatePwdContract.View {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.et_origin_pwd)
    EditText etOriginPwd;
    @BindView(R.id.et_new_pwd)
    EditText etNewPwd;
    @BindView(R.id.et_confirm_pwd)
    EditText etConfirmPwd;
    @BindView(R.id.update_pwd_button)
    TextView updatePwdButton;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(cn.mumzone.basic.R.anim.right_in, cn.mumzone.basic.R.anim.right_out);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.gravity = Gravity.RIGHT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(params);
        getWindow().setBackgroundDrawableResource(R.color.bg_color);
    }

    @Override
    protected void initEventAndData() {
        // 限制空格与换行
        TrimAllEnterAndSpaceFilters trimAllEnterAndSpaceFilter = new TrimAllEnterAndSpaceFilters();
        // 输入长度控制
        InputFilter.LengthFilter maxLengthFilter = new InputFilter.LengthFilter(20);
        etOriginPwd.setFilters(new InputFilter[]{trimAllEnterAndSpaceFilter, maxLengthFilter});
        etNewPwd.setFilters(new InputFilter[]{trimAllEnterAndSpaceFilter, maxLengthFilter});
        etConfirmPwd.setFilters(new InputFilter[]{trimAllEnterAndSpaceFilter, maxLengthFilter});

        etOriginPwd.requestFocus();
    }

    @Override
    public void showUpdatePwdSuccess() {
        ToastUtil.makeText(mContext, "修改成功", false);
        finish();
    }

    @Override
    protected int getLayout() {
        return R.layout.act_update_pwd;
    }

    @OnClick({R.id.iv_back, R.id.iv_close, R.id.update_pwd_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                ACT_Setting.startAc(mContext);
                finish();
                break;
            case R.id.iv_close:
                finish();
                break;
            case R.id.update_pwd_button:
                updatePwd();
                break;
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(cn.mumzone.basic.R.anim.right_in, cn.mumzone.basic.R.anim.right_out);
    }

    private void updatePwd() {
        String originPwd = etOriginPwd.getText().toString().trim();
        String newPwd = etNewPwd.getText().toString().trim();
        String newPwdAgain = etConfirmPwd.getText().toString().trim();

        if (checkChangePwdAndVerify(newPwd, newPwdAgain)) {
            mPresenter.updatePwd(originPwd, newPwd);
        }
    }

    private boolean checkChangePwdAndVerify(String newPwd, String newPwdAgain) {
        //检验两次密码是否一致
        if (!newPwd.equals(newPwdAgain)) {
            ToastUtil.makeText(mContext, "两次密码输入不一致，请重新输入", false);
            return false;
        }
        //校验密码6-20位
        if (!PatternUtils.checkPassWord(newPwd)) {
            ToastUtil.makeText(mContext, "密码格式不正确", false);
            return false;
        }
        //校验密码6-20位
        if (!PatternUtils.checkPassWordLength(newPwdAgain)) {
            ToastUtil.makeText(mContext, "请输入6-20位字符", false);
            return false;
        }
        return true;
    }

    // 回车/空格 过滤器
    private class TrimAllEnterAndSpaceFilters extends LoginFilter.UsernameFilterGMail {

        public TrimAllEnterAndSpaceFilters() {
            super();
        }

        @Override
        public boolean isAllowed(char c) {
            if (' ' != c && c != '\n')
                return true;
            return false;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        ACT_Setting.startAc(mContext);
    }

    public static void startAc(Context context) {
        Intent intent = new Intent(context, ACT_UpdatePwd.class);
        context.startActivity(intent);
    }
}
